<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!-- ref="formRef"获取当前表单元素 -->
      <el-form ref="formRef" :model="form" label-width="80px" :rules="formRules">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户姓名" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="输入密码" show-password prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="form.repassword"
            placeholder="确认密码"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="el-buttom" type="primary" @click="handleReg">注册</el-button>
        </el-form-item>
        <!-- 与router文件夹中，routes一致 -->
        <el-link type="info" @click="$router.push('/login')">去登陆</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { registerAPI } from '@/api/register.js'
export default {
  name: 'Register',
  data() {
    // 自定义，确认密码的校验规则函数
    const validateRepass = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        // 与后台需要的字段一致
        username: '',
        password: '',
        repassword: ''
      },
      formRules: {
        username: [
          { required: true, message: '请输用户', trigger: 'blur' },
          // 与后台需要的字段一致
          { pattern: /^[a-z\d]{2,10}$/i, message: '字母数字长度1-10', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          // 密码和确认密码一致，借助自定义校验
          { validator: validateRepass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      console.log('submit!')
    },
    // 注册
    handleReg() {
      // 1.兜底函数
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // 2.通过兜底校验了，可以发请求了
        const {
          data: { code, message }
        } = await registerAPI(this.form)
        console.log(code, message)
        if (code === 0) {
          this.$message.success(message)
          this.$router.push('/login')
        } else {
          this.$message.warning(message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  background: url('~@/assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .register-box {
    //后台管理系统框
    width: 400px;
    height: 370px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }
    .el-buttom {
      //注册框
      margin-top: 20px;
      width: 100%;
    }
    .el-link.is-underline:hover:after {
      //去登录链接下面的hover伪元素边框消除
      border-bottom: none;
    }
  }
}
</style>
